package net.ge.mosq.web.servlet.form;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import net.ge.mosq.utils.Utils;
import net.ge.mosq.web.others.ActionMessages;
import net.ge.mosq.web.others.EnvironmentInitialization;

import org.apache.commons.validator.Validator;
import org.apache.commons.validator.ValidatorException;
import org.apache.commons.validator.ValidatorResources;
import org.apache.commons.validator.ValidatorResults;

/**
 * @author noodles.wumu@gmail.com
 * @since 2012/07/27 13:23:06
 * @version 1.0
 */
public class ValidatorForm extends Form {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5557106372877873884L;

	/**
	 * Resources key the <code>ServletContext</code> is stored under.
	 */
	private static String SERVLET_CONTEXT_PARAM = "javax.servlet.ServletContext";

	/**
	 * Resources key the <code>HttpServletRequest</code> is stored under.
	 */
	private static String HTTP_SERVLET_REQUEST_PARAM = "javax.servlet.http.HttpServletRequest";

	/**
	 * Resources key the <code>ActionMessages</code> is stored under.
	 */
	private static String ACTION_MESSAGES_PARAM = "net.ge.mosq.web.others.ActionMessages";

	/**
	 * The results returned from the validation performed by the <code>Validator</code>.
	 */
	protected ValidatorResults validatorResults = null;

	/**
	 * Used to indicate the current page of a multi-page form.
	 */
	protected int page = 0;

	/**
	 * Gets page.
	 * 
	 * @return page number
	 */
	public int getPage() {
		return page;
	}

	/**
	 * Sets page.
	 * 
	 * @param page
	 *            page number
	 */
	public void setPage(int page) {
		this.page = page;
	}

	/**
	 * Validate the properties that have been set from this HTTP request, and return an <code>ActionErrors</code> object that encapsulates any validation errors that have been
	 * found. If no errors are found, return <code>null</code> or an <code>ActionErrors</code> object with no recorded error messages.
	 * 
	 * @param request
	 *            The servlet request we are processing
	 * @return <code>ActionErrors</code> object that encapsulates any validation errors
	 */
	public ActionMessages validate(HttpServletRequest request) {

		ServletContext application = request.getSession().getServletContext();
		ActionMessages errors = new ActionMessages();

		Validator validator = new Validator((ValidatorResources) application.getAttribute(EnvironmentInitialization.VALIDATOR_KEY), this.getClass().getName());
		validator.setUseContextClassLoader(true);

		validator.setPage(page);
		validator.setParameter(SERVLET_CONTEXT_PARAM, application);
		validator.setParameter(HTTP_SERVLET_REQUEST_PARAM, request);
		validator.setParameter(Validator.LOCALE_PARAM, Utils.getUserLocale(request));
		validator.setParameter(ACTION_MESSAGES_PARAM, errors);
		validator.setParameter(Validator.BEAN_PARAM, this);

		try {
			validatorResults = validator.validate();
			request.setAttribute(ActionMessages.GLOBAL_MESSAGE, errors);
		} catch (ValidatorException e) {
			log.error(e.getMessage(), e);
		}
		return errors;
	}
}
